package com.wang.controller.admin;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.wang.entity.User;
import com.wang.service.UserService;
import com.wang.controller.util.JWTUtil;
import com.wang.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname AdminUserController
 * @Description TODO
 * @Date 2024/8/29 9:34
 * @Created by pc
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/admin/user")
@Api(value = "后台用户接口",tags = "AdminUserController")
public class AdminUserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "用户登录",notes = "后台用户登录")
    @ApiImplicitParam(value = "用户对象数据",name = "user",required = true,dataType = "User")
    public Map<String, Object> login(@RequestBody User user){
        // ResponseUtil r = userService.adminAddUser(user);
        ResponseUtil r = userService.adminAddUser(user);
        Map<String,Object> map = new HashMap<>();
        if(r.getCode().intValue() == 1012){
            // 登录成功
            Map<String,String> payload = new HashMap<>();
            // 携带用户信息 payload
            User loginUser = (User) r.getData();
            payload.put("username",loginUser.getUsername());
            payload.put("userId",loginUser.getId().toString());
            payload.put("roleId",loginUser.getRole().toString());
            // 生成JWT
            String token = JWTUtil.getToken(payload);
            map.put("status",true);
            map.put("msg","登录成功");
            map.put("token",token);
        }else{
            // 登录失败
            map.put("status",false);
            map.put("msg",r.getMsg());
        }

        return map;
    }

    // 测试令牌接口
    @GetMapping("/test")
    public Map<String,Object> test(String token){
        Map<String,Object> map = new HashMap<>();
        try {
            DecodedJWT decodedJWT = JWTUtil.verify(token);
            // 没有异常 验签通过
            map.put("state", true);
            map.put("msg", "请求成功");
            return map;
        } catch (SignatureVerificationException e) {
            e.printStackTrace();
            map.put("msg", "无效签名!");
        } catch (TokenExpiredException e) {
            e.printStackTrace();
            map.put("msg", "token过期!");
        } catch (AlgorithmMismatchException e) {
            e.printStackTrace();
            map.put("msg", "token算法不一致!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "token无效");
        }
        map.put("state", false);
        return map;
    }


    @GetMapping("/test2")
    public Map<String, Object> test2(String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("state", true);
        map.put("msg", "请求成功");
        return map;
    }

    @GetMapping("/findAll")
    @ApiOperation(value = "获取所有用户",notes = "后台获取所有用户的详细信息")
    public ResponseUtil findAll(){
        return userService.findAll();
    }

    @GetMapping("/find/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页获取所有用户",notes = "后台获取所有用户的详细信息")
    public ResponseUtil find(@PathVariable("pageNum")Integer pageNum,
                             @PathVariable("pageSize")Integer pageSize){
        return userService.find(pageNum,pageSize);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加用户",notes = "后台添加用户信息")
    @ApiImplicitParam(value = "用户对象数据",name = "user",required = true,dataType = "User")
    public ResponseUtil add(@RequestBody User user){
        return userService.addUser(user);
    }


}
